package com.lanlengran.lancher.utils;

import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Parcelable;
import android.util.Log;

/**
 * Created by ruiqin on 17-1-17.
 */

public class NetUtils {
    public static boolean getWifiState(Intent intent){
        // 监听wifi的连接状态即是否连上了一个有效无线路由
        Parcelable parcelableExtra = intent
                .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (null != parcelableExtra) {
            // 获取联网状态的NetWorkInfo对象
            NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
            //获取的State对象则代表着连接成功与否等状态
            NetworkInfo.State state = networkInfo.getState();
            //判断网络是否已经连接
            boolean isConnected = state == NetworkInfo.State.CONNECTED;
            Log.e("TAG", "isConnected:" + isConnected);
            if (isConnected) {
               return true;
            } else {
                return false;
            }
        }
        return false;
    }
}
